

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="叶尘的技术升级小木屋">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>5、控制语句 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="5、控制语句">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-09-13 22:01" pubdate>
        星期一, 九月 13日 2021, 10:01 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.7k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      21
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">5、控制语句</h1>
            
            <div class="markdown-body">
              <h3 id="5-1-if语句"><a href="#5-1-if语句" class="headerlink" title="5.1 if语句"></a>5.1 if语句</h3><p><strong>if 语句是分支语句，也可以叫做条件语句。</strong></p>
<ul>
<li><p>语法格式</p>
<ul>
<li>第一种</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span>(布尔表达式)&#123;<br>    java语句;<br>    java语句;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>第二种</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span>(布尔表达式)&#123;  <span class="hljs-comment">// 分支1</span><br>    java语句;     <br>&#125;<span class="hljs-keyword">else</span>&#123;            <span class="hljs-comment">// 分支2</span><br>    java语句;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>第三种</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">1</span>)&#123; <span class="hljs-comment">// 分支1</span><br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">2</span>)&#123; <span class="hljs-comment">// 分支2</span><br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">3</span>)&#123;<br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">4</span>)&#123;<br>    java语句;<br>&#125;....<br></code></pre></td></tr></table></figure>

<ul>
<li>第四种</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">1</span>)&#123; <span class="hljs-comment">// 分支1</span><br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">2</span>)&#123; <span class="hljs-comment">// 分支2</span><br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">3</span>)&#123;<br>    java语句;<br>&#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(布尔表达式<span class="hljs-number">4</span>)&#123;<br>    java语句;<br>&#125;<span class="hljs-keyword">else</span>&#123;<br>    java语句; <span class="hljs-comment">// 以上条件没有一个成立的。这个else就执行了。</span><br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>对于 if 语句来说，在任何情况下只能有 1 个分支执行，不可能存在 2 个或者更多个分支执行。if 语句中只要有 1 个分支执行了，整个 if 语句就结束了。（对于 1 个完整的if语句来说的。）</p>
</li>
<li><p>以上 4 种语法机制中，凡是带有 else 分支的，一定可以保证会有一个分支执行。以上 4 种当中，第一种和第三种没有 else 分支，这样的语句可能会导致最后一个分支都不执行。第二种和第四种肯定会有 1个分支执行。</p>
</li>
<li><p>当分支当中“java语句;”只有1条，那么大括号{}可以省略，但为了可读性，最好不要省略。</p>
</li>
<li><p>控制语句和控制语句之间是可以嵌套的，但是嵌套的时候大家最好一个语句一个语句进行分析，不要冗杂在一起分析。</p>
</li>
<li><p>一个例子</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> java.util.Scanner;<br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span> </span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br>	<span class="hljs-comment">/*</span><br><span class="hljs-comment">	业务要求：</span><br><span class="hljs-comment">		1、从键盘上接收一个人的年龄。</span><br><span class="hljs-comment">		2、年龄要求为[0-150]，其它值表示非法，需要提示非法信息。</span><br><span class="hljs-comment">		3、根据人的年龄来动态的判断这个人属于生命的哪个阶段？</span><br><span class="hljs-comment">			[0-5] 婴幼儿</span><br><span class="hljs-comment">			[6-10] 少儿</span><br><span class="hljs-comment">			[11-18] 少年</span><br><span class="hljs-comment">			[19-35] 青年</span><br><span class="hljs-comment">			[36-55] 中年</span><br><span class="hljs-comment">			[56-150] 老年</span><br><span class="hljs-comment">		4、请使用if语句完成以上的业务逻辑。</span><br><span class="hljs-comment">	*/</span><br>	String str = <span class="hljs-string">&quot;老年&quot;</span>; <span class="hljs-comment">// 字符串变量默认值是“老年”</span><br>		<span class="hljs-keyword">if</span>(age &lt; <span class="hljs-number">0</span> || age &gt; <span class="hljs-number">150</span>)&#123;<br>			System.out.println(<span class="hljs-string">&quot;对不起，年龄值不合法&quot;</span>);<br>			<span class="hljs-comment">// 既然不合法，你就别让程序往下继续执行了，怎么终止程序执行</span><br>			<span class="hljs-comment">//return;</span><br>		&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(age &lt;= <span class="hljs-number">5</span>)&#123;<br>			str = <span class="hljs-string">&quot;婴幼儿&quot;</span>;<br>		&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(age &lt;= <span class="hljs-number">10</span>)&#123;<br>			str = <span class="hljs-string">&quot;少儿&quot;</span>;<br>		&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(age &lt;= <span class="hljs-number">18</span>)&#123;<br>			str = <span class="hljs-string">&quot;少年&quot;</span>;<br>		&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(age &lt;= <span class="hljs-number">35</span>)&#123;<br>			str = <span class="hljs-string">&quot;青年&quot;</span>;<br>		&#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(age &lt;= <span class="hljs-number">55</span>)&#123;<br>			str = <span class="hljs-string">&quot;中年&quot;</span>;<br>		&#125; <br>		System.out.println(str);	<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="5-2-switch语句"><a href="#5-2-switch语句" class="headerlink" title="5.2 switch语句"></a>5.2 switch语句</h3><p><strong>switch语句也是选择语句，也可以叫做分支语句</strong></p>
<ul>
<li>语法格式: </li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">switch</span>(值)&#123;<br>    <span class="hljs-keyword">case</span> 值<span class="hljs-number">1</span>:<br>        java语句;<br>        java语句;...<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">case</span> 值<span class="hljs-number">2</span>:<br>        java语句;<br>        java语句;...<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">case</span> 值<span class="hljs-number">3</span>:<br>        java语句;<br>        java语句;...<br>        <span class="hljs-keyword">break</span>;<br>    <span class="hljs-keyword">default</span>:<br>        java语句;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>其中：break;语句不是必须的。default分支也不是必须的</p>
<ul>
<li>switch语句支持的值有哪些:<ul>
<li>支持 int 类型以及 String 类型</li>
<li>在 JDK8 之前不支持 String 类型</li>
<li>byte , short , char 也可以使用在 switch 语句当中，因为 byte  short  char 可以进行自动类型转换</li>
</ul>
</li>
<li>switch 语句执行原理：<ul>
<li>拿“值”与“值1”进行比较，如果相同，则执行该分支中的 java 语句，然后遇到 “break;” 语句，switch语句就结束了。</li>
<li>如果“值”与“值1”不相等，会继续拿“值”与“值2”进行比较，如果相同，则执行该分支中的 java 语句，然后遇到 “break;” 语句，switch 结束。</li>
<li><strong>注意：如果分支执行了，但是分支最后没有 “break;”，此时会发生<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=286">case 穿透现象</a> 视频09:36处。</strong></li>
<li>所有的 case 都没有匹配成功，那么最后 default 分支会执行。</li>
</ul>
</li>
</ul>
<h3 id="5-3-for循环"><a href="#5-3-for循环" class="headerlink" title="5.3 for循环"></a>5.3 for循环</h3><ul>
<li>语法机制：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">for</span>(初始化表达式; 条件表达式; 更新表达式)&#123;<br>    循环体; <span class="hljs-comment">// 循环体由java语句构成</span><br>    java语句;<br>    java语句;<br>    java语句;<br>    java语句;<br>    ....<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><p>执行原理:</p>
<ol>
<li>先执行初始化表达式，并且初始化表达式只执行1次。</li>
<li>然后判断条件表达式的结果，如果条件表达式结果为true，则执行循环体。</li>
<li>循环体结束之后，执行更新表达式。</li>
<li>更新完之后，再判断条件表达式的结果，如果还是true，继续执行循环体。</li>
<li>直到更新表达式执行结束之后，再次判断条件时，条件为false，for循环终止。</li>
</ol>
</li>
<li><p>更新表达式的作用是：控制循环的次数。</p>
</li>
<li><p>最简练的for循环怎么写?</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">for</span>(;;)&#123;<br>    System.out.println(<span class="hljs-string">&quot;死循环&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>这就是个死循环</p>
<ul>
<li>输出 0 - 9</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;i &lt; <span class="hljs-number">10</span>;i++)&#123;<br>    System.out.println(<span class="hljs-string">&quot;i = &quot;</span> + i); <span class="hljs-comment">// 0 1 2 3....9</span><br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>使用for循环，实现1~100所有奇数求和</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span> sum = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">100</span>; i += <span class="hljs-number">2</span>) &#123;<br>    sum += i;<br>&#125;<br>System.out.println(<span class="hljs-string">&quot;sum = &quot;</span> + sum);<span class="hljs-comment">// sum = 2500</span><br></code></pre></td></tr></table></figure>

<ul>
<li>九九乘法表</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span> </span>&#123;<br>	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>&#123;<br>		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">9</span>; i++) &#123;<span class="hljs-comment">// 纵向循环</span><br>			<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt;= i; j++) &#123;<span class="hljs-comment">// 横向循环，j的范围受i的影响</span><br>				System.out.print(i +<span class="hljs-string">&quot;*&quot;</span>+j +<span class="hljs-string">&quot;=&quot;</span>+(i*j)+<span class="hljs-string">&quot;\t&quot;</span>);<br>			&#125;<br>			System.out.println();<br>		&#125;<br>	&#125;	<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="5-4-while循环"><a href="#5-4-while循环" class="headerlink" title="5.4 while循环"></a>5.4 while循环</h3><ul>
<li>语法机制：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">while</span>(布尔表达式)&#123;<br>    循环体;<br>    更新表达式;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li><p>执行原理：</p>
<ol>
<li><p>判断布尔表达式的结果，如果为true就执行循环体，更新表达式</p>
</li>
<li><p>再次判断布尔表达式的结果，如果还是true，继续执行循环体，直到布尔表达式结果为false，while循环结束。</p>
</li>
</ol>
</li>
<li><p><strong>while循环的循环次数是：0~n次</strong></p>
</li>
</ul>
<h3 id="5-5-do-while循环"><a href="#5-5-do-while循环" class="headerlink" title="5.5 do..while循环"></a>5.5 do..while循环</h3><ul>
<li>语法机制</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">do</span> &#123;<br>    循环体;<br>&#125;<span class="hljs-keyword">while</span>(布尔表达式);<br></code></pre></td></tr></table></figure>

<ul>
<li>执行原理：<ol>
<li>先执行循环体当中的代码，执行一次循环体之后，判断布尔表达式的结果</li>
<li>如果为true，则继续执行循环体，如果为false循环结束</li>
</ol>
</li>
<li><strong>对于do..while循环来说循环体执行次数是：1~n次。</strong></li>
</ul>
<h3 id="5-6-break-语句"><a href="#5-6-break-语句" class="headerlink" title="5.6 break;语句"></a>5.6 break;语句</h3><ul>
<li>break;语句可以用在哪里呢<ol>
<li>switch 语句当中，用来终止 switch 语句的执行。用在 switch 语句当中，防止 case 穿透现象，用来**==终止 switch==**。</li>
<li>break;语句用在循环语句当中，**==用来终止离它最近的那个循环语句==**。</li>
</ol>
</li>
<li>一种特殊的语法（不怎么用）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java">a:<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> k = <span class="hljs-number">0</span>; k &lt; <span class="hljs-number">2</span>; k++)&#123; <br>    b:<span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++)&#123;<br>        <span class="hljs-keyword">if</span>(i == <span class="hljs-number">5</span>)&#123;<br>            <span class="hljs-keyword">break</span> a; <span class="hljs-comment">// 终止指定的循环，即整个大循环。</span><br>        &#125;<br>        System.out.println(<span class="hljs-string">&quot;i ===&gt; &quot;</span> + i); <br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="5-7-continue-语句"><a href="#5-7-continue-语句" class="headerlink" title="5.7 continue;语句"></a>5.7 continue;语句</h3><ul>
<li>continue语句的作用是：**==终止当前”本次”循环，直接进入下一次循环继续执行。==**</li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E6%96%B9%E6%B3%95%E4%B8%8E%E9%80%92%E5%BD%92/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">6、方法与递归</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E8%BF%90%E7%AE%97%E7%AC%A6/">
                        <span class="hidden-mobile">4、运算符</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
